//==========================================  
//
//  @ Project : Algoritmos Geneticos
//  @ File Name : Crossover.java
//  @ Date : 25/08/2010
//  @ Author : Leanderson Andr�
//
//==========================================  

package sistemaClassificador.algoritmoGenetico;

import java.util.List;
import java.util.Random;

import util.Logger;


public class Crossover
{	private Logger log = new Logger(this);
	public void operar(Regra r1, Regra r2, List<Regra> listaCandidata,MersenneTwister geradorRandom) throws Exception
	{
		Regra c1 = new Regra(r1.size());
		Regra c2 = new Regra(r2.size());
		int pontoCorte = (new Double(geradorRandom.nextDouble()*r1.size())).intValue();
//		log.info("Ponto de Corte"+pontoCorte);
		double rand = geradorRandom.nextDouble();
		 if(rand<0.5) {
			for(int a=0;a<pontoCorte;a++){
				c1.addCondicao(r1.getCondicao(a));
				c2.addCondicao(r2.getCondicao(a));
			}
			for(int a=pontoCorte;a<r1.size();a++){
				c1.addCondicao(r2.getCondicao(a));	
				c2.addCondicao(r1.getCondicao(a));
			}
			c1.setClasse(r1.getClasse());				
			c2.setClasse(r2.getClasse());
		}else{
			for(int a=0;a<pontoCorte;a++){
				c1.addCondicao(r2.getCondicao(a));
				c2.addCondicao(r1.getCondicao(a));
			}
			for(int a=pontoCorte;a<r1.size();a++){
				c1.addCondicao(r1.getCondicao(a));	
				c2.addCondicao(r2.getCondicao(a));
			}
			c1.setClasse(r2.getClasse());				
			c2.setClasse(r1.getClasse());
		}
		c1.setTaxaCobertura(0);
		c2.setTaxaCobertura(0);
		c1.setFitness(0);
		c2.setFitness(0);
		
		if(!listaCandidata.contains(c1)) listaCandidata.add(c1);
		if(!listaCandidata.contains(c2)) listaCandidata.add(c2);


//		log.info("NovaRegra01 : Fitness:"+c1.getFitness()+" Classe : "+c1.getClasse());
//		for(int x=0;x<c1.size();x++){
//			log.info("Atributo: "+x+ c1.getCondicao(x));
//		}
//		log.info("NovaRegra02 : Fitness:"+c2.getFitness()+" Classe : "+c2.getClasse());
//		for(int x=0;x<c2.size();x++){
//			log.info("Atributo: "+x+ c2.getCondicao(x));
//		}
	}
}
